package com.example.springtransdemo.Controller;

import com.example.springtransdemo.Mapper.LogInfoMapper;
import com.example.springtransdemo.Service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManagerr;
    @Autowired
    private TransactionDefinition definition;
    @RequestMapping("/r1")
    public String r1(String username, String password) {
        TransactionStatus transactionStatus = dataSourceTransactionManagerr.getTransaction(definition);
        userService.insertLog(username,password);
        log.info("用户登录成功");
        dataSourceTransactionManagerr.commit(transactionStatus);
        return "success";
    }
    @RequestMapping("/r2")
    public String r2(String username, String password) {
        TransactionStatus transactionStatus = dataSourceTransactionManagerr.getTransaction(definition);
        userService.insertLog(username,password);
        log.info("用户注册成功");
        dataSourceTransactionManagerr.rollback(transactionStatus);
        return "success";
    }
}
